<template>
    <Layout>
        <div class="NavBar">
            <Icon class="leftIcon" name="left" @click="goBack"/>
            <span class="title">编辑标签</span>
            <Icon class="rightIcon"/>
        </div>
        <div class="form-wrapper">
            <FormItem :value="currentTag.name"
                      @update:value="update"
                      field-name="标签名" placeholder="点击输入标签名"/>
        </div>
        <div class="button-wrapper">
            <Button @click="remove">删除标签</Button>
        </div>
    </Layout>
</template>

<script lang="ts">
    import Vue from 'vue';
    import {Component} from 'vue-property-decorator';
    import FormItem from '@/components/Money/FormItem.vue';
    import Button from '@/components/Button.vue';

    @Component({
        components: {Button, FormItem},
        // computed:{
        //     tag(){
        //         return this.$store.state.currenwtTag
        //     }
        // }  //根据vue class component文档直接改写成原生的get方法
    })
    export default class EditLabel extends Vue {

        get currentTag(){
            return this.$store.state.currentTag
        }

        created() {
            const id=this.$route.params.xxx
            //每次进来就fetch一下数据
            this.$store.commit('fetchTags')
            this.$store.commit('setCurrentTag',id)

            //commit的返回值为空 不能通过commit获取到数据
           //this.tag=this.$store.commit('findTag',this.$route.params.xxx)

            if (!this.currentTag) {
                this.$router.replace('/404')
            }
        }

        update(name: string) {
            if (this.currentTag) {
                this.$store.commit('updateTag',{
                    id:this.currentTag.id,name})
            }
        }

        remove() {
            if (this.currentTag) {
                this.$store.commit('removeTag',this.currentTag.id)
            }
        }

        goBack() {
            this.$router.back()  //这个back略有bug
        }
    }
</script>

<style lang="scss" scoped>
    .NavBar {
        text-align: center;
        font-size: 16px;
        padding: 12px 16px;
        background: #ffff;
        display: flex;
        align-items: center;
        justify-content: space-between;

        > .title {

        }

        > .leftIcon {
            width: 24px;
            height: 24px;
        }

        > .rightIcon {
            width: 24px;
            height: 24px;
        }
    }

    .form-wrapper {
        border-top: 1px solid #e4e4e4;
    }

    .button-wrapper {
        text-align: center;
        padding: 16px;
        margin-top: 44-16px;
    }

</style>
